<!--
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<kd-card role="table"
         [expanded]="expanded"
         [hidden]="isHidden()">
  <div title
       fxLayout="row"
       i18n>Events</div>
  <div description><span class="kd-muted-light"
          i18n>Items:&nbsp;</span>{{totalItems}}</div>

  <div content
       [hidden]="showZeroState()">
    <div kdLoadingSpinner
         [isLoading]="isLoading"></div>

    <mat-table [dataSource]="getData()"
               [trackBy]="trackByResource"
               matSort
               matSortActive="lastSeen"
               matSortDisableClear
               matSortDirection="asc">
      <ng-container matColumnDef="statusicon">
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let event">
          <mat-icon [ngClass]="getStatus(event).iconClass"
                    [matTooltip]="getStatus(event).iconTooltip">
            {{getStatus(event).iconName}}
          </mat-icon>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="name">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-md"
                         i18n>Name</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-md">{{event.objectMeta.name}}</mat-cell>
      </ng-container>

      <ng-container *ngIf="shouldShowColumn('namespace')"
                    matColumnDef="namespace">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-sm"
                         i18n>Namespace</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-sm">{{event.objectMeta.namespace}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="reason">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         class="kd-col-xs"
                         i18n>Reason</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-xs">{{event.reason}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="message">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-gt"
                         i18n>Message</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-gt">{{event.message}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="source">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-md"
                         i18n>Source</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-md">
          {{event.sourceComponent}} {{event.sourceHost}}
        </mat-cell>
      </ng-container>

      <ng-container *ngIf="shouldShowColumn('object')"
                    matColumnDef="object">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-md"
                         i18n>Object</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-md">
          <div *ngIf="event.objectName">
            <a *ngIf="getObjectHref(event.objectKind, event.objectName, event.objectNamespace); else unsupportedObject"
               [routerLink]="getObjectHref(event.objectKind, event.objectName, event.objectNamespace)"
               queryParamsHandling="preserve">
              {{event.objectKind}}/{{event.objectName}}
            </a>
            <ng-template #unsupportedObject>{{event.objectKind}}/{{event.objectName}}</ng-template>
          </div>
          <div *ngIf="!event.objectName">-</div>
        </mat-cell>
      </ng-container>

      <ng-container *ngIf="shouldShowColumn('subobject')"
                    matColumnDef="subobject">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-md"
                         i18n>Sub-object</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-md">
          <div *ngIf="event.object">{{event.object}}</div>
          <div *ngIf="!event.object">-</div>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="count">
        <mat-header-cell *matHeaderCellDef
                         class="kd-col-xs"
                         i18n>Count</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-xs">{{event.count}}</mat-cell>
      </ng-container>

      <ng-container matColumnDef="firstSeen">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         class="kd-col-sm"
                         i18n>First Seen</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-sm">
          <kd-date [date]="event.firstSeen"
                   [relative]="true"></kd-date>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="lastSeen">
        <mat-header-cell *matHeaderCellDef
                         mat-sort-header
                         class="kd-col-sm"
                         i18n>Last Seen</mat-header-cell>
        <mat-cell *matCellDef="let event"
                  class="kd-col-sm">
          <kd-date [date]="event.lastSeen"
                   [relative]="true"></kd-date>
        </mat-cell>
      </ng-container>

      <ng-container *ngFor="let col of getActionColumns()"
                    [matColumnDef]="col.name">
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let event">
          <kd-dynamic-cell [component]="col.component"
                           [resource]="event"></kd-dynamic-cell>
        </mat-cell>
      </ng-container>

      <mat-header-row *matHeaderRowDef="getColumns()"></mat-header-row>
      <mat-row #matrow
               *matRowDef="let row; columns: getColumns()"></mat-row>
    </mat-table>

    <div [hidden]="totalItems <= itemsPerPage">
      <mat-paginator [length]="totalItems"
                     [pageSize]="itemsPerPage"
                     hidePageSize
                     showFirstLastButtons></mat-paginator>
    </div>
  </div>

  <div content
       [hidden]="!showZeroState()">
    <kd-list-zero-state></kd-list-zero-state>
  </div>
</kd-card>
